//
//  JSBusiness.m
//  JSKit
//
//  Created by Apple on 2019/7/21.
//  Copyright © 2019 Apple. All rights reserved.
//

#import "JSBusiness.h"
#import <QMUIKit/QMUIKit.h>
#import <YTKNetwork.h>
#import <AFNetworking.h>
#import <AFNetworkActivityIndicatorManager.h>

static NSUInteger totalTime = 0;

@interface JSBusiness ()

@property (nonatomic, strong) dispatch_source_t timer;

@end

@implementation JSBusiness

- (instancetype)init{
    if (self = [super init]) {
#ifdef DEBUG
        _isDebug = YES;
#else
        _isDebug = NO;
#endif
        /// 网络监听
//        [self startNetWorkMonitoring];
        /// 开启全局定时器
        [self startTimer];
        NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
        /// 系统版本号
        _systemVersion =  [NSString stringWithFormat:@"%.2f",[[[UIDevice currentDevice] systemVersion]   floatValue]];
        
        /// app 版本
        _appVersion =  [infoDictionary objectForKey:@"CFBundleShortVersionString"];
        
        /// build 版本
        _buildVersion = [infoDictionary objectForKey:@"CFBundleVersion"];
        
        /// 手机型号
        _phoneModel =  [[UIDevice currentDevice] model];
        
        /// app 名称
        _appName =  [infoDictionary objectForKey:@"CFBundleDisplayName"];
        
        _bundleId = [infoDictionary objectForKey:@"CFBundleIdentifier"];
    }
    return self;
}
-(void)startNetWorkMonitoring{
    /*! 1.获得网络监控的管理者 */
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    /*! 当使用AF发送网络请求时,只要有网络操作,那么在状态栏(电池条)wifi符号旁边显示  菊花提示 */
    [AFNetworkActivityIndicatorManager sharedManager].enabled = YES;
    /*! 2.设置网络状态改变后的处理 */
    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        /*! 当网络状态改变了, 就会调用这个block */
        JSNetworkStatus networkStatus = 0;
        switch (status)
        {
            case AFNetworkReachabilityStatusUnknown:
                networkStatus = JSNetworkStatusUnknown;
                break;
            case AFNetworkReachabilityStatusNotReachable:
                networkStatus = JSNetworkStatusNotReachable;
                break;
            case AFNetworkReachabilityStatusReachableViaWWAN:
                networkStatus = JSNetworkStatusReachableViaWWAN;
                break;
            case AFNetworkReachabilityStatusReachableViaWiFi:
                networkStatus = JSNetworkStatusReachableViaWiFi;
                break;
        }
        if (_currentNetworkStatus == JSNetworkStatusReachableViaWiFi && networkStatus == JSNetworkStatusReachableViaWWAN) {
            return ;
        }
        _currentNetworkStatus = networkStatus;
        self.currentNetworkStatusObj = [NSNumber numberWithInteger:networkStatus];
        if ([self.delegate respondsToSelector:@selector(appNetWorkDidChange:)]) {
            [self.delegate appNetWorkDidChange:networkStatus];
        }
    }];
    [manager startMonitoring];
}
-(void)startTimer{
    totalTime = 0;
    /** 获取一个全局的线程来运行计时器*/
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    /** 创建一个计时器*/
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    /** 设置计时器, 这里是每10毫秒执行一次*/
    dispatch_source_set_timer(timer, dispatch_walltime(nil, 0), 1000*NSEC_PER_MSEC, 0);
    /** 设置计时器的里操作事件*/
    dispatch_source_set_event_handler(timer, ^{
        //do you want....
        if ([self.delegate respondsToSelector:@selector(appGroupTimer)]) {
            [self.delegate appGroupTimer];
            totalTime += 1;
        }
    });
    dispatch_resume(timer);
    self.timer = timer;
}
- (NSUInteger)runTime{
    return totalTime;
}
+ (instancetype)sharedInstance {
    static dispatch_once_t onceToken;
    static JSBusiness *instance = nil;
    dispatch_once(&onceToken,^{
        instance = [[super allocWithZone:NULL] init];
        instance.qmui_multipleDelegatesEnabled = YES;
    });
    return instance;
}

+ (id)allocWithZone:(struct _NSZone *)zone{
    return [self sharedInstance];
}

@end
